from django.shortcuts import render
from django.views import View

from contents.models import ContentCategory
# from goods.models import GoodsChannel
from meiduo_mall.utils.meiduo_category import get_categories


class IndexView(View):
    def get(self, request):
            # 1.查询频道分类信息，调用
            categories = get_categories()

            # 2.查询广告信息
            contents = ContentCategory.objects.all()
            contents_dict = {}
            for content in contents:
                contents_dict[content.key] = content.content_set.filter(status=True).order_by('sequence')

            context = {
                'categories': categories,
                'contents': contents_dict
            }
            return render(request, 'index.html', context)

"""channels= GoodsChannel.objects.order_by('group_id','sequence')
# 建立一个字典，上传给前端
catetories = {}
for channel in channels:
#每次遍历出一个完整的频道，总共11个频道数据[(),(),()] >>>{}
# catetories[1] = {手机}
# catetories[1] = {相机}
# catetories[1] = {数码}
if channel.group_id not in catetories:   #表示如果字典里面没有这个数据
    catetories[channel.group_id]={
        'channels' : [],        #一级分类的列表
        'sub_cats' : []        #二级分类的列表

    }
    # 获取当前的频道
    channel_dict = catetories[channel.group_id]
    channel_dict['channels'].append({
        'name':channel.categroy.name,
        'url':channel.url

    })   #追加一级分类的名字，通过categorey字段的关联关系
         # 拿到分类表的字段信息
    """

